/**
 * Created by lixiaoping on 15/10/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'model',
    'dialog'
], function ($, common, ready, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            loading();
            //地区
            $.ajax({
                url: _global.url.api + 'regions',
                type: 'get',
                async: false,
                data: {depth: 2},
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('region', res.data);
                    }
                }
            });
            this.fetch();
            this.event();
        },

        fetch: function () {
            var that = this;
            this.model = new Model({
                url: _global.url.api + 'withdraw_info',
                render: function () {
                    $('#main-view').html(template('edit-tpl', {
                        region: that.region,
                        bank_list: this.data.bank_list,
                        withdraw_info: this.data.withdraw_info
                    }));
                },
                callback: function () {
                    loaded();
                }
            });
            this.model.fetch();

            loaded();
        },
        set: function ($k, $v) {
            this[$k] = $v;
        },

        event: function () {
            var _this = this;
            //console.log(this.model.data);
            /**
             * 添加事件
             */

                //保存事件
            $('#btn-save').on('click', function (e) {
                e.preventDefault();
                var data = {};
                data.province = $('[name="province"]').val();
                data.city = $('[name="city"]').val();
                data.bank = $('[name="bank"]').val();
                data.agency_name = $('input[name="agency_name"]').val();
                data.account = $('input[name="account"]').val();
                data.name = $('input[name="name"]').val();
                data.bank_sn = $('input[name="bank_sn"]').val();

                var tmpModel = new Model({
                    url: _global.url.api + 'withdraw_info',
                    data: data
                });
                tmpModel.put();
            });

            //地区联动
            $('.province').on('change', function () {
                $('select[name="city"]').html('');
                var id = $(this).val();
                var city = _this.region[id].child;
                for (var n in city) {
                    $('select[name="city"]').append('<option value="' + city[n].id + '">' + city[n].name + '</option>')
                }
            });
            if (_this.model.data.withdraw_info.province > 0) {
                var city = _this.region[_this.model.data.withdraw_info.province].child;
                for (var n in city) {
                    var selected = '';
                    if (_this.model.data.withdraw_info.city == city[n].id) {
                        selected = 'selected';
                    }
                    $('select[name="city"]').append('<option value="' + city[n].id + '" ' + selected + '>' + city[n].name + '</option>')
                }
            }
        }
    };

    new index();

});